package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.workstation.CreateWorkstationRequestDto;
import org.dxy.api.dto.request.workstation.UpdateWorkstationRequestDto;
import org.dxy.api.response.R;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.query.WorkstationBasicViewPo;
import org.dxy.trigger.http.service.WorkstationApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工位管理控制器
 * @author dxy
 * @createTime 2025/9/4
 */
@Slf4j
@RestController
@RequestMapping("/api/workstation")
@RequiredArgsConstructor
public class WorkstationController {

    private final WorkstationApplicationService workstationApplicationService;

    /**
     * @description 创建工位
     * @param dto 包含工位创建信息的请求DTO
     * @return 返回创建成功或失败的响应
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @PostMapping("/create")
    public R<Void> createWorkstation(@Validated @RequestBody CreateWorkstationRequestDto dto) {
        return workstationApplicationService.createWorkstation(dto);
    }

    /**
     * @description 分页查询工位列表（支持条件过滤）
     * @param page 分页参数（页码、每页大小等）
     * @param query 过滤条件（工位名称、状态、产线等）
     * @return 分页的工位列表响应
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @GetMapping("/list")
    public R<Page<WorkstationBasicViewPo>> list(
            @ParameterObject Page<WorkstationBasicViewPo> page,
            @ParameterObject WorkstationBasicViewPo query) {
        return workstationApplicationService.list(page, query);
    }

    /**
     * @description 更新工位信息
     * @param id 工位ID
     * @param dto 包含更新信息的请求DTO
     * @return 返回更新成功或失败的响应
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody UpdateWorkstationRequestDto dto) {
        return workstationApplicationService.update(id, dto);
    }

    /**
     * @description 删除单个工位
     * @param id 工位ID
     * @return 返回删除成功或失败的响应
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        return workstationApplicationService.delete(id);
    }

    /**
     * @description 批量删除工位
     * @param ids 工位ID列表
     * @return 返回批量删除成功或失败的响应
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @DeleteMapping("/batch")
    public R<Void> batchDelete(@RequestBody List<Long> ids) {
        return workstationApplicationService.batchDelete(ids);
    }

    /**
     * @description 获取工位详情
     * @param id 工位ID
     * @return 返回工位详情信息
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @GetMapping("/{id}")
    public R<WorkstationBasicViewPo> getById(@PathVariable Long id) {
        return workstationApplicationService.getById(id);
    }

    /**
     * @description 根据产线ID获取工位列表
     * @param productionLineId 产线ID
     * @return 返回工位列表
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @GetMapping("/productionLine/{productionLineId}")
    public R<List<WorkstationBasicViewPo>> getByProductionLineId(@PathVariable Long productionLineId) {
        return workstationApplicationService.getByProductionLineId(productionLineId);
    }

    /**
     * @description 根据产线ID获取启用状态的工位列表
     * @param productionLineId 产线ID
     * @return 返回启用状态的工位列表
     * @date 2025/9/4 20:01
     * @auther dxy
     */
    @GetMapping("/productionLine/{productionLineId}/enabled")
    public R<List<WorkstationBasicViewPo>> getEnabledByProductionLineId(@PathVariable Long productionLineId) {
        return workstationApplicationService.getEnabledByProductionLineId(productionLineId);
    }
}